package stp2;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class LaunchPlayer {

	private static String sep = System.getProperty("file.separator");
	
	public static void play(Download file){
		
		
		String title = "Flash FLV Player";
		
		String style = "\t\t<style type=\"text/css\">\n" +
		"\t\t\tbody { margin: 50px; font:12/18px Arial; background: black; }\n" +
		"\t\t\th2 { font-weight: normal; color: #C00; }\n" +
		"\t\t\tul, li { padding: 0; list-style: none; }\n" +
		"\t\t\tli a {\n" +
		"\t\t\t\tdisplay: block;\n" +
		"\t\t\t\tbackground: #EEE;\n" +
		"\t\t\t\ttext-decoration: none;\n" +
		"\t\t\t\tcolor: #000;\n" +
		"\t\t\t\tpadding: 5px;\n" +
		"\t\t\t\twidth: 470px;\n" +
		"\t\t\t}\n" +
		"\t\t\tli .alt { background:#FFF; }\n" +
		"\t\t\tli a:hover {  background: #C00; color: #FFF; }\n" +
		"\t\t</style>\n";
		
		
		
		String script = "\t\t<script type=\"text/javascript\">\n" +
		"\t\t\tfunction playMovie(file) {\n" +
		"\t\t\t\tvar FO = {\n" +
		"\t\t\t\t\tmovie:\"flvplayer.swf\", \n" +
		"\t\t\t\t\twidth:\"480px\", \n" +
		"\t\t\t\t\theight:\"380px\",\n " +
		"\t\t\t\t\tmajorversion:\"7\", \n" +
		"\t\t\t\t\tbuild:\"0\", \n" +
		"\t\t\t\t\tbgcolor:\"#FFFFFF\", \n" +
		"\t\t\t\t\tflashvars:\"file=\"+file+\"&autoStart=true\"\n" +
		"\t\t\t\t};\n" +
		"\t\t\t\tUFO.create(FO, 'player');\n" +
		"\t\t\t}\n";
		if(file != null)
		script += "\t\tplayMovie('../videos/"+file.getName()+".flv');\n";
		script += "\t\t</script>\n";
		
		String path = DownloadManager.path; /*System.getProperty("java.class.path");
		if(path.endsWith("\\YDM.jar")){
			path = path.replace("\\YDM.jar", "");
		}
		if(path.startsWith("YDM.jar")) path = "";*/
		File dir = new File(path+sep+"videos"+sep);
		String[] videos = dir.list();
		
		String playlist = "\t\t<ul>\n";
		
		for(int i = 0; i< videos.length; i++){
			if(videos[i].endsWith(".flv"))
				playlist += "\t\t\t<li><a href=\"javascript:playMovie('../videos/"+videos[i]+"')\">"+videos[i].substring(0, videos[i].length()-4)+"</a></li>\n";
		}
		playlist += "\t\t</ul>\n";

		
		
		String html = "<html>\n" +
				"\t<head>\n" +
				"\t\t<title>"+title+"</title>\n" +
				"\t\t<script type=\"text/javascript\" src=\"ufo.js\"></script>\n" +
				style +
				"\t</head>\n" +
				"\t<body>\n" +
				"\t\t<h2>"+title+"</h2>\n" +
				"\t\t<div id=\"player\"></div>\n" +
				"\t\t<h2>Playlist:</h2>\n" +
				playlist +
				script +
				"\t</body>\n" +
				"</html>";
		
		
		
		path = DownloadManager.path; /*System.getProperty("java.class.path");
		if(path.endsWith("\\YDM.jar")){
			path = path.replace("\\YDM.jar", "");
		}
		if(path.startsWith("YDM.jar")) path = "";*/
		String player_url = path+sep+"player"+sep+"flvplayer.html";
		File f = new File(player_url);
		try {
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
			dos.writeUTF(html);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*path = System.getProperty("java.class.path");
		if(path.startsWith("YDM.jar")) BareBonesBrowserLaunch.openURL(player_url);
		else*/
		BareBonesBrowserLaunch.openURL("file://"+player_url);      
	}
	
}
